﻿using System;
using System.Collections.Generic;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	internal class AuxiliaryCurveDel
	{
		public AuxiliaryCurveDel(Autodesk.Revit.DB.Document curDoc)
		{
			this.m_CurDoc = curDoc;
		}

		public void Del()
		{
			this.DeleteAuxiliaryCurve(this.m_CurDoc, "boostLineInfo", true);
		}

		private List<string> GetCurveIds(string alGuid)
		{
			List<string> list = new List<string>();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(this.pathName);
				foreach (object obj in xmlDocument.SelectSingleNode("ListDetailLine").ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (!(xmlNode.Attributes["dlType"].Value.ToString() != alGuid))
					{
						list.Add(xmlNode.InnerText);
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		private void DeleteAuxiliaryCurve(Autodesk.Revit.DB.Document document, string alGuid, bool beBoostLine)
		{
			List<string> curveIds = this.GetCurveIds(alGuid);
			if (curveIds.Count <= 0 && beBoostLine)
			{
				YJKMessageBox.Show("该视图没有您所要\n删除的辅助线！");
				return;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfClass(typeof(CurveElement));
			List<Element> list = new List<Element>();
			foreach (Element element in filteredElementCollector)
			{
				foreach (string b in curveIds)
				{
					if (element.Id.ToString() == b)
					{
						list.Add(element);
					}
				}
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document);
			filteredElementCollector2.OfClass(typeof(ReferencePlane));
			foreach (Element element2 in filteredElementCollector2)
			{
				foreach (string b2 in curveIds)
				{
					if (element2.Id.ToString() == b2)
					{
						list.Add(element2);
					}
				}
			}
			if (list.Count <= 0)
			{
				YJKMessageBox.Show("该视图没有您所要\n删除的辅助线！");
				return;
			}
			foreach (Element elem in list)
			{
				try
				{
					document.DeleteElement(elem);
				}
				catch
				{
				}
			}
			this.DeleteCurveIds(alGuid);
		}

		private void DeleteCurveIds(string alGuid)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(this.pathName);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("ListDetailLine");
				List<XmlNode> list = new List<XmlNode>();
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					if (!(xmlNode2.Attributes["dlType"].Value.ToString() != alGuid))
					{
						list.Add(xmlNode2);
					}
				}
				foreach (XmlNode oldChild in list)
				{
					xmlNode.RemoveChild(oldChild);
				}
				xmlDocument.Save(this.pathName);
			}
			catch (Exception)
			{
				throw;
			}
		}

		private string pathName = Product.InstallLocation + "\\Data\\HYSprinklerCheck.XML";

		private Document m_CurDoc;
	}
}
